# -*- coding: utf-8 -*-

"""
@author: 猿小天
@contact: QQ:1638245306
@Created on: 2021/6/3 003 0:30
@Remark: 用户管理
"""
import hashlib

from django.contrib.auth.hashers import make_password
from rest_framework import serializers
from rest_framework.permissions import IsAuthenticated

from ops.config.models import Project
from dvadmin.utils.json_response import SuccessResponse, ErrorResponse
from dvadmin.utils.serializers import CustomModelSerializer
from dvadmin.utils.validator import CustomUniqueValidator
from dvadmin.utils.viewset import CustomModelViewSet


class ProjectSerializer(CustomModelSerializer):
    """
    项目管理-序列化器
    """

    class Meta:
        model = Project
        fields = '__all__'
        read_only_fields = ["id"]


class ProjectCreateUpdateSerializer(CustomModelSerializer):
    """
    项目新增-序列化器
    """
    name = serializers.CharField(max_length=50, validators=[CustomUniqueValidator(queryset=Project.objects.all(), message="项目名称必须唯一")])

    class Meta:
        model = Project
        fields = "__all__"
        read_only_fields = ["id"]


class ProjectViewSet(CustomModelViewSet):
    """
    项目管理接口
    list:查询
    create:新增
    update:修改
    retrieve:单例
    destroy:删除
    """
    permission_classes = [IsAuthenticated]
    queryset = Project.objects.all()
    serializer_class = ProjectSerializer
    search_fields = ['name', 'mark']
    create_serializer_class = ProjectCreateUpdateSerializer
    update_serializer_class = ProjectCreateUpdateSerializer


